package com.itheima.mp.domain.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

@Data
@ApiModel(description = "分页查询实体")
public class PageQuery {
    @ApiModelProperty("页码")
    private Long pageNo = 1L;
    @ApiModelProperty("每页条数")
    private Long pageSize = 5L;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = false;

    public <T> Page<T> toMpPage(OrderItem... orderItems) {
        //分页条件
        Page<T> page = Page.of(pageNo, pageSize);
        //排序条件,倒叙
        if (StringUtils.hasText(sortBy)) {
            //排序字段不为空
            page.addOrder(new OrderItem(sortBy, isAsc));
        } else if (orderItems != null) {
            //默认排序
            page.addOrder(orderItems);
        }
        return page;
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTime() {
        return this.toMpPage(new OrderItem("create_time", false));
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTime() {
        return this.toMpPage(new OrderItem("update_time", false));
    }
}